/*
 * @Author: HanYaJun
 * @Date: 2023-01-06 15:29:25
 * @Email: hanyajun@wedobest.com.cn
 * @Description: 屏幕适配
 */

const { ccclass, property } = cc._decorator;

@ccclass
export default class AdaptationCom extends cc.Component {

    @property({ type: cc.Node, displayName: "顶部适配ui" })
    private topUI: cc.Node = null;

    @property({ type: cc.Node, displayName: "底部适配ui" })
    private boyttomUI: cc.Node = null;

    @property({ displayName: "顶部适配ui距离顶部的偏移" })
    private topUiDistance: number = 0;

    @property({ displayName: "底部适配ui距离底部的偏移" })
    private bottomUiDistance: number = 0;

    private recordPosY: number = 0;

    protected onEnable(): void {
        // cc.winSize和cc.view.getVisibleSize()的返回值是一样的，二者可以通用
        let screenSize: cc.Size = cc.view.getVisibleSize();
        // 返回手机屏幕安全区域（设计分辨率为单位），如果不是异形屏将默认返回 visibleRect。目前支持安卓、iOS 原生平台和微信小游戏平台
        let safeSize: cc.Size = cc.sys.getSafeAreaRect().size;

        if (safeSize.height != screenSize.height) {
            this.topUiDistance += 20;
            this.offsetY = 20 + (screenSize.height - safeSize.height) / 2;
        }

        let halfHight = safeSize.height / 2;

        if (this.topUI) {
            this.topUI.y = halfHight - this.topUiDistance;
        }

        if (this.boyttomUI) {
            let tempHight = screenSize.height / 2;
            this.boyttomUI.y = -tempHight + this.bottomUiDistance;
        }
    }

    protected update(dt: number): void {
        if (!this.topUI) {
            return;
        }

        let pos: cc.Vec2 = this.topUI.getPosition();
        if (this.recordPosY != pos.y) {
            this.recordPosY = pos.y;
            console.log("【HYJ】屏幕适配的y坐标:", pos.y);
        }
    }

    /**异形屏顶部的y坐标偏移值 */
    private _offsetY: number = 0;
    public get offsetY(): number {
        return this._offsetY;
    }

    public set offsetY(value: number) {
        this._offsetY = value;
    }
}